package com.rj.data.common;

import com.google.common.io.Files;
import com.google.common.io.LineProcessor;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.*;

/**
 * 全局任务广播变量
 */
public class BroadCastMap {
    static String BASE_INFO_DIR = "/data/combase";
    static String[] files = new String[]{
            "行政处罚主表-信用中国.txt",
            "经营异常名录-工商局-190224.txt",
            "失信被执行人.txt",
            "严重违法失信-黑名单-工商局-190224.txt",
            "欠税公告-181118.txt",
            "失信采购不良记录.txt",
            "失信税务处罚.txt"};

    private final static List<Map<Integer, Integer>> fields = new ArrayList<>();

    static Map<Integer, Integer> load(String file) throws Exception {
        System.out.println("broadcast file : " + file);
        Map<Integer, Integer> maps = new HashMap<>();
        return Files.asCharSource(new File(BASE_INFO_DIR + File.separator + file), Charset.forName("UTF-8")).readLines(new LineProcessor<Map<Integer, Integer>>() {
            @Override
            public boolean processLine(String l) throws IOException {
                String[] tarr = l.split("\t");
                maps.put(Integer.parseInt(tarr[0]), Integer.parseInt(tarr[1]));
                return true;
            }

            @Override
            public Map<Integer, Integer> getResult() {
                return maps;
            }
        });

    }

    public static void broadMap() {

        Arrays.stream(files).forEach(file -> {
            {
                try {

                    fields.add(load(file));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public static Map<Integer, Integer> getFieldMap(Integer offset) {
        return fields.get(offset);
    }

    public static List<Map<Integer, Integer>> getFieldMap() {
        return fields;
    }
}
